package com.zh.note.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhanghuan
 * @date: 2023/9/13 15:48
 * @description: create it
 */
@Aspect
public class LogAspect {

    public Map<String, Integer> map = new HashMap<>();

    @Pointcut("execution(* com.zh.note.service.*.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void doBefore(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName + "方法执行前...");
        if (map.get(methodName) == null) {
            map.put(methodName, 1);
        } else {
            map.put(methodName, map.get(methodName) + 1);
        }
    }

    @After("pointCut()")
    public void doAfter(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName + "方法执行后...");
        System.out.println(methodName + "执行次数为:" + map.get(methodName));
    }

}
